home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdSpellCheck.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  13.6 KB  |  517 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  */
  22.  
  23. var gMisspelledWord;
  24. var gSpellChecker;
  25. var gAllowSelectWord = true;
  26. var gPreviousReplaceWord = "";
  27. var gFirstTime = true;
  28. var gSendMailMessageMode = false;
  29.  
  30. // dialog initialization code
  31. function Startup()
  32. {
  33.   if (!InitEditorShell())
  34.     return;
  35.  
  36.   // Get the spellChecker shell
  37.   gSpellChecker = editorShell.QueryInterface(Components.interfaces.nsIEditorSpellCheck);
  38.   if (!gSpellChecker) {
  39.     dump("SpellChecker not found!!!\n");
  40.     window.close();
  41.     return;
  42.   }
  43.  
  44.   // Start the spell checker module.
  45.   try {
  46.    gSpellChecker.InitSpellChecker();
  47.  
  48.    // XXX: We need to read in a pref here so we can set the
  49.    //      default language for the spellchecker!
  50.    // gSpellChecker.SetCurrentDictionary();
  51.   }
  52.   catch(ex) {
  53.    dump("*** Exception error: InitSpellChecker\n");
  54.     window.close();
  55.     return;
  56.   }
  57.  
  58.   gDialog.MisspelledWordLabel = document.getElementById("MisspelledWordLabel");
  59.   gDialog.MisspelledWord      = document.getElementById("MisspelledWord");
  60.   gDialog.ReplaceButton       = document.getElementById("Replace");
  61.   gDialog.IgnoreButton        = document.getElementById("Ignore");
  62.   gDialog.StopButton          = document.getElementById("Stop");
  63.   gDialog.CloseButton         = document.getElementById("Close");
  64.   gDialog.ReplaceWordInput    = document.getElementById("ReplaceWordInput");
  65.   gDialog.SuggestedList       = document.getElementById("SuggestedList");
  66.   gDialog.LanguageMenulist    = document.getElementById("LanguageMenulist");
  67.  
  68.   // Fill in the language menulist and sync it up
  69.   // with the spellchecker's current language.
  70.  
  71.   var curLang;
  72.  
  73.   try {
  74.     curLang = gSpellChecker.GetCurrentDictionary();
  75.   } catch(ex) {
  76.     curLang = "";
  77.   }
  78.  
  79.   InitLanguageMenu(curLang);
  80.   
  81.   // Get the first misspelled word and setup all UI
  82.   NextWord();
  83.  
  84.   // When startup param is true, setup different UI when spell checking 
  85.   //   just before sending mail message  
  86.   gSendMailMessageMode = window.arguments[0];
  87.   if (gSendMailMessageMode)
  88.   {
  89.     // If no misspelled words found, simply close dialog and send message
  90.     if (!gMisspelledWord)
  91.     {
  92.       onClose();
  93.       return;
  94.     }
  95.  
  96.     // "Close" button becomes "Send"
  97.     gDialog.CloseButton.setAttribute("label", GetString("Send"));
  98.   }
  99.   else
  100.   {
  101.     // Normal spell checking - hide the "Stop" button
  102.     // (Note that this button is the "Cancel" button for
  103.     //  Esc keybinding and related window close actions)
  104.     gDialog.StopButton.setAttribute("hidden", "true");
  105.   }
  106.  
  107.   // Clear flag that determines message when
  108.   //  no misspelled word is found
  109.   //  (different message when used for the first time)
  110.   gFirstTime = false;
  111. }
  112.  
  113. function InitLanguageMenu(curLang)
  114. {
  115.  
  116.   var o1 = {};
  117.   var o2 = {};
  118.  
  119.   // Get the list of dictionaries from
  120.   // the spellchecker.
  121.  
  122.   try {
  123.     gSpellChecker.GetDictionaryList(o1, o2);
  124.   } catch(ex) {
  125.     dump("Failed to get DictionaryList!\n");
  126.     return;
  127.   }
  128.  
  129.   var dictList = o1.value;
  130.   var count    = o2.value;
  131.  
  132.   // Load the string bundles that will help us map
  133.   // RFC 1766 strings to UI strings.
  134.  
  135.   var languageBundle;
  136.   var regionBundle;
  137.   var menuStr2;
  138.   var isoStrArray;
  139.   var defaultIndex = 0;
  140.   var langId;
  141.  
  142.   // Try to load the language string bundle.
  143.  
  144.   try {
  145.     languageBundle = srGetStrBundle("chrome://global/locale/languageNames.properties");
  146.   } catch(ex) {
  147.     languageBundle = null;
  148.   }
  149.  
  150.   // If we have a language string bundle, try to load the region string bundle.
  151.  
  152.   if (languageBundle)
  153.   {
  154.     try {
  155.       regionBundle = srGetStrBundle("chrome://global/locale/regionNames.properties");
  156.     } catch(ex) {
  157.       regionBundle = null;
  158.     }
  159.   }
  160.   var i;
  161.   for (i = 0; i < dictList.length; i++)
  162.   {
  163.     try {
  164.       langId = dictList[i];
  165.       isoStrArray = dictList[i].split("-");
  166.  
  167.       dictList[i] = new Array(2); // first subarray element - pretty name
  168.       dictList[i][1] = langId;    // second subarray element - language ID
  169.  
  170.       if (languageBundle && isoStrArray[0])
  171.         dictList[i][0] = languageBundle.GetStringFromName(isoStrArray[0].toLowerCase());
  172.  
  173.       if (regionBundle && dictList[i][0] && isoStrArray.length > 1 && isoStrArray[1])
  174.       {
  175.         menuStr2 = regionBundle.GetStringFromName(isoStrArray[1].toLowerCase());
  176.         if (menuStr2)
  177.           dictList[i][0] = dictList[i][0] + "/" + menuStr2;
  178.       }
  179.  
  180.       if (!dictList[i][0])
  181.         dictList[i][0] = dictList[i][1];
  182.     } catch (ex) {
  183.       // GetStringFromName throws an exception when
  184.       // a key is not found in the bundle. In that
  185.       // case, just use the original dictList string.
  186.  
  187.       dictList[i][0] = dictList[i][1];
  188.     }
  189.   }
  190.   
  191.   // note this is not locale-aware collation, just simple ASCII-based sorting
  192.   // we really need to add loacel-aware JS collation, see bug XXXXX
  193.   dictList.sort();
  194.  
  195.   for (i = 0; i < dictList.length; i++)
  196.   {
  197.     AppendLabelAndValueToMenulist(gDialog.LanguageMenulist, dictList[i][0], dictList[i][1]);
  198.     if (curLang && dictList[i][1] == curLang)
  199.       defaultIndex = i+2; //first two items are pre-populated and fixed
  200.   }
  201.  
  202.   // Now make sure the correct item in the menu list is selected.
  203.  
  204.   if (dictList.length > 0)
  205.     gDialog.LanguageMenulist.selectedIndex = defaultIndex;
  206. }
  207.  
  208. function DoEnabling()
  209. {
  210.   if (!gMisspelledWord)
  211.   {
  212.     // No more misspelled words
  213.     gDialog.MisspelledWord.setAttribute("value",GetString( gFirstTime ? "NoMisspelledWord" : "CheckSpellingDone"));
  214.  
  215.     gDialog.ReplaceButton.removeAttribute("default");
  216.     gDialog.IgnoreButton.removeAttribute("default");
  217.  
  218.     gDialog.CloseButton.setAttribute("default","true");
  219.     // Shouldn't have to do this if "default" is true?
  220.     gDialog.CloseButton.focus();
  221.  
  222.     SetElementEnabledById("MisspelledWordLabel", false);
  223.     SetElementEnabledById("ReplaceWordLabel", false);
  224.     SetElementEnabledById("ReplaceWordInput", false);
  225.     SetElementEnabledById("CheckWord", false);
  226.     SetElementEnabledById("SuggestedListLabel", false);
  227.     SetElementEnabledById("SuggestedList", false);
  228.     SetElementEnabledById("Ignore", false);
  229.     SetElementEnabledById("IgnoreAll", false);
  230.     SetElementEnabledById("Replace", false);
  231.     SetElementEnabledById("ReplaceAll", false);
  232.     SetElementEnabledById("AddToDictionary", false);
  233.   } else {
  234.     SetElementEnabledById("MisspelledWordLabel", true);
  235.     SetElementEnabledById("ReplaceWordLabel", true);
  236.     SetElementEnabledById("ReplaceWordInput", true);
  237.     SetElementEnabledById("CheckWord", true);
  238.     SetElementEnabledById("SuggestedListLabel", true);
  239.     SetElementEnabledById("SuggestedList", true);
  240.     SetElementEnabledById("Ignore", true);
  241.     SetElementEnabledById("IgnoreAll", true);
  242.     SetElementEnabledById("AddToDictionary", true);
  243.  
  244.     gDialog.CloseButton.removeAttribute("default");
  245.     SetReplaceEnable();
  246.   }
  247. }
  248.  
  249. function NextWord()
  250. {
  251.   gMisspelledWord = gSpellChecker.GetNextMisspelledWord();
  252.   SetWidgetsForMisspelledWord();
  253. }
  254.  
  255. function SetWidgetsForMisspelledWord()
  256. {
  257.   gDialog.MisspelledWord.setAttribute("value", TruncateStringAtWordEnd(gMisspelledWord, 30, true));
  258.  
  259.  
  260.   // Initial replace word is misspelled word
  261.   gDialog.ReplaceWordInput.value = gMisspelledWord;
  262.   gPreviousReplaceWord = gMisspelledWord;
  263.  
  264.   // This sets gDialog.ReplaceWordInput to first suggested word in list
  265.   FillSuggestedList(gMisspelledWord);
  266.  
  267.   DoEnabling();
  268.  
  269.   if (gMisspelledWord)
  270.     SetTextboxFocus(gDialog.ReplaceWordInput);
  271. }
  272.  
  273. function CheckWord()
  274. {
  275.   word = gDialog.ReplaceWordInput.value;
  276.   if (word) 
  277.   {
  278.     isMisspelled = gSpellChecker.CheckCurrentWord(word);
  279.     if (isMisspelled)
  280.     {
  281.       FillSuggestedList(word);
  282.       SetReplaceEnable();
  283.     } 
  284.     else 
  285.     {
  286.       ClearListbox(gDialog.SuggestedList);
  287.       var item = gDialog.SuggestedList.appendItem(GetString("CorrectSpelling"), "");
  288.       if (item) item.setAttribute("disabled", "true");
  289.       // Suppress being able to select the message text
  290.       gAllowSelectWord = false;
  291.     }
  292.   }
  293. }
  294.  
  295. function SelectSuggestedWord()
  296. {
  297.   if (gAllowSelectWord)
  298.   {
  299.     var selectedItem
  300.     if (gDialog.SuggestedList.selectedItem)
  301.     {
  302.       var selValue = gDialog.SuggestedList.selectedItem.getAttribute("label");
  303.       gDialog.ReplaceWordInput.value = selValue;
  304.       gPreviousReplaceWord = selValue;
  305.     }
  306.     else
  307.     {
  308.       gDialog.ReplaceWordInput.value = gPreviousReplaceWord;
  309.     }
  310.     SetReplaceEnable();
  311.   }
  312. }
  313.  
  314. function ChangeReplaceWord()
  315. {
  316.   // Calling this triggers SelectSuggestedWord(),
  317.   //  so temporarily suppress the effect of that
  318.   var saveAllow = gAllowSelectWord;
  319.   gAllowSelectWord = false;
  320.  
  321.   // Select matching word in list
  322.   var newIndex = -1;
  323.   var newSelectedItem;
  324.   var replaceWord = TrimString(gDialog.ReplaceWordInput.value);
  325.   if (replaceWord)
  326.   {
  327.     for (var i = 0; i < gDialog.SuggestedList.getRowCount(); i++)
  328.     {
  329.       var item = gDialog.SuggestedList.getItemAtIndex(i);
  330.       if (item.getAttribute("label") == replaceWord)
  331.       {
  332.         newSelectedItem = item;
  333.         break;
  334.       }
  335.     }
  336.   }
  337.   gDialog.SuggestedList.selectedItem = newSelectedItem;
  338.  
  339.   gAllowSelectWord = saveAllow;
  340.  
  341.   // Remember the new word
  342.   gPreviousReplaceWord = gDialog.ReplaceWordInput.value;
  343.  
  344.   SetReplaceEnable();
  345. }
  346.  
  347. function Ignore()
  348. {
  349.   NextWord();
  350. }
  351.  
  352. function IgnoreAll()
  353. {
  354.   if (gMisspelledWord) {
  355.     gSpellChecker.IgnoreWordAllOccurrences(gMisspelledWord);
  356.   }
  357.   NextWord();
  358. }
  359.  
  360. function Replace()
  361. {
  362.   var newWord = gDialog.ReplaceWordInput.value;
  363.   if (gMisspelledWord && gMisspelledWord != newWord)
  364.   {
  365.     editorShell.BeginBatchChanges();
  366.     var isMisspelled = gSpellChecker.ReplaceWord(gMisspelledWord, newWord, false);
  367.     editorShell.EndBatchChanges();
  368.   }
  369.   NextWord();
  370. }
  371.  
  372. function ReplaceAll()
  373. {
  374.   var newWord = gDialog.ReplaceWordInput.value;
  375.   if (gMisspelledWord && gMisspelledWord != newWord)
  376.   {
  377.     editorShell.BeginBatchChanges();
  378.     isMisspelled = gSpellChecker.ReplaceWord(gMisspelledWord, newWord, true);
  379.     editorShell.EndBatchChanges();
  380.   }
  381.   NextWord();
  382. }
  383.  
  384. function AddToDictionary()
  385. {
  386.   if (gMisspelledWord) {
  387.     gSpellChecker.AddWordToDictionary(gMisspelledWord);
  388.   }
  389.   NextWord();
  390. }
  391.  
  392. function EditDictionary()
  393. {
  394.   window.openDialog("chrome://editor/content/EdDictionary.xul", "_blank", "chrome,close,titlebar,modal", "", gMisspelledWord);
  395. }
  396.  
  397. function SelectLanguage()
  398. {
  399.   try {
  400.     var item = gDialog.LanguageMenulist.selectedItem;
  401.     if (item.value != "more-cmd")
  402.       gSpellChecker.SetCurrentDictionary(item.value);
  403.     else
  404.       window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no,modal", xlateURL('urn:clienturl:composer:spellcheckers'));
  405.   } catch (ex) {
  406.     dump(ex);
  407.   }
  408. }
  409.  
  410. function Recheck()
  411. {
  412.   //TODO: Should we bother to add a "Recheck" method to interface?
  413.   try {
  414.     var curLang = gSpellChecker.GetCurrentDictionary();
  415.  
  416.     gSpellChecker.UninitSpellChecker();
  417.     gSpellChecker.InitSpellChecker();
  418.     gSpellChecker.SetCurrentDictionary(curLang);
  419.     gMisspelledWord = gSpellChecker.GetNextMisspelledWord();
  420.     SetWidgetsForMisspelledWord();
  421.   } catch(ex) {
  422.     dump(ex);
  423.   }
  424. }
  425.  
  426. function FillSuggestedList(misspelledWord)
  427. {
  428.   var list = gDialog.SuggestedList;
  429.  
  430.   // Clear the current contents of the list
  431.   gAllowSelectWord = false;
  432.   ClearListbox(list);
  433.   var item;
  434.  
  435.   if (misspelledWord.length > 0)
  436.   {
  437.     // Get suggested words until an empty string is returned
  438.     var count = 0;
  439.     var firstWord = 0;
  440.     do {
  441.       var word = gSpellChecker.GetSuggestedWord();
  442.       if (count==0)
  443.         firstWord = word;
  444.       if (word.length > 0)
  445.       {
  446.         list.appendItem(word, "");
  447.         count++;
  448.       }
  449.     } while (word.length > 0);
  450.  
  451.     if (count == 0)
  452.     {
  453.       // No suggestions - show a message but don't let user select it
  454.       item = list.appendItem(GetString("NoSuggestedWords"));
  455.       if (item) item.setAttribute("disabled", "true");
  456.       gAllowSelectWord = false;
  457.     } else {
  458.       gAllowSelectWord = true;
  459.       // Initialize with first suggested list by selecting it
  460.       gDialog.SuggestedList.selectedIndex = 0;
  461.     }
  462.   } 
  463.   else
  464.   {
  465.     item = list.appendItem("", "");
  466.     if (item)
  467.       item.setAttribute("disabled", "true");
  468.   }
  469. }
  470.  
  471. function SetReplaceEnable()
  472. {
  473.   // Enable "Change..." buttons only if new word is different than misspelled
  474.   var newWord = gDialog.ReplaceWordInput.value;
  475.   var enable = newWord.length > 0 && newWord != gMisspelledWord;
  476.   SetElementEnabledById("Replace", enable);
  477.   SetElementEnabledById("ReplaceAll", enable);
  478.   if (enable)
  479.   {
  480.     gDialog.ReplaceButton.setAttribute("default","true");
  481.     gDialog.IgnoreButton.removeAttribute("default");
  482.   }
  483.   else
  484.   {
  485.     gDialog.IgnoreButton.setAttribute("default","true");
  486.     gDialog.ReplaceButton.removeAttribute("default");
  487.   }
  488. }
  489.  
  490. function doDefault()
  491. {
  492.   if (gDialog.ReplaceButton.getAttribute("default") == "true")
  493.     Replace();
  494.   else if (gDialog.IgnoreButton.getAttribute("default") == "true")
  495.     Ignore();
  496.   else if (gDialog.CloseButton.getAttribute("default") == "true")
  497.     onClose();
  498.  
  499.   return false;
  500. }
  501.  
  502. function CancelSpellCheck()
  503. {
  504.   gSpellChecker.UninitSpellChecker();
  505.  
  506.   // Signal to calling window that we canceled
  507.   window.opener.cancelSendMessage = true;
  508.   return true;
  509. }
  510.  
  511. function onClose()
  512. {
  513.   gSpellChecker.UninitSpellChecker();
  514.   window.opener.cancelSendMessage = false;
  515.   window.close();
  516. }
  517.